Use the [Serializable] attribute or subclassing from MarshalByRefObject?
Posted
by Theo Lenndorff
on Stack Overflow
See other posts from Stack Overflow
or by Theo Lenndorff
Published on 2009-03-01T12:14:30Z
Indexed on
2010/03/16
21:41 UTC
Read the original article
Hit count: 325
I'd like to use an object across AppDomains.
For this I can use the [Serializeable] attribute:
[Serializable]
class MyClass
{
public string GetSomeString() { return "someString" }
}
Or subclass from MarshalByRefObject:
class MyClass: MarshalByRefObject
{
public string GetSomeString() { return "someString" }
}
In both cases I can use the class like this:
AppDomain appDomain = AppDomain.CreateDomain("AppDomain");
MyClass myObject = (MyClass)appDomain.CreateInstanceAndUnwrap(
typeof(MyClass).Assembly.FullName,
typeof(MyClass).FullName);
Console.WriteLine(myObject.GetSomeString());
Why do both approaches seem to have the same effect? What is the difference in both approaches? When should I favor the one approach over the other?
EDIT: At the surface I know that there are differences between both mechanisms, but if someone jumped out of a bush and asked me the question I couldn't give him a proper answer. The questions are quite open questions. I hoped that someone can explain it better than I could do.
© Stack Overflow or respective owner